package com.xinqi.modules.sys.complaint.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.sys.complaint.convert.ComplaintConfigConvert;
import com.xinqi.modules.sys.complaint.domain.ComplaintConfigEntity;
import com.xinqi.modules.sys.complaint.dto.req.ComplaintConfigCreateDTO;
import com.xinqi.modules.sys.complaint.dto.req.ComplaintConfigDeleteDTO;
import com.xinqi.modules.sys.complaint.dto.req.ComplaintConfigQueryDTO;
import com.xinqi.modules.sys.complaint.dto.req.ComplaintConfigUpdateDTO;
import com.xinqi.modules.sys.complaint.dto.rsp.ComplaintConfigResultDTO;
import com.xinqi.modules.sys.complaint.manager.ComplaintConfigManager;
import com.xinqi.modules.sys.complaint.service.ComplaintConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户投诉规则表服务层实现
 *
 * @author: yyzhenn
 * @date: 2023/04/23
 */
@Validated
@Service("complaintConfigService")
@RequiredArgsConstructor
public class ComplaintConfigServiceImpl implements ComplaintConfigService {
    private final ComplaintConfigConvert complaintConfigConvert;
    private final ComplaintConfigManager complaintConfigManager;

    /**
     * 分页查询 用户投诉规则表数据
     */
    @Override
    @DS("slaver")
    public PageInfo<ComplaintConfigResultDTO> page(Param pageable, ComplaintConfigQueryDTO query) {
        IPage<ComplaintConfigEntity> page = complaintConfigManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, complaintConfigConvert::convert);
    }

    /**
     * 查询 用户投诉规则表数据
     */
    @Override
    public List<ComplaintConfigResultDTO> find(ComplaintConfigQueryDTO query) {
        return complaintConfigManager.list(wrapper(query)).stream()
            .map(complaintConfigConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<ComplaintConfigEntity> wrapper(ComplaintConfigQueryDTO query) {
        LambdaQueryWrapper<ComplaintConfigEntity> wrapper = Queries.lambda();
        Queries.accept(query.getName(), v -> wrapper.like(ComplaintConfigEntity::getName, v));
        Queries.accept(query.getDisableFeature(), v -> wrapper.eq(ComplaintConfigEntity::getDisableFeature, v));
        wrapper.orderByDesc(ComplaintConfigEntity::getCreateTime);
        return wrapper;
    }

    /**
     * 根据id查询 用户投诉规则表数据
     */
    @Override
    public ComplaintConfigResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("complaint.config.id.not.blank"));
        return Optional.ofNullable(complaintConfigManager.getById(id)).map(complaintConfigConvert::convert).orElse(null);
    }

    /**
     * 新增 用户投诉规则表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(ComplaintConfigCreateDTO dto) {
        ComplaintConfigEntity entity = complaintConfigConvert.create(dto);
        complaintConfigManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 用户投诉规则表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(ComplaintConfigUpdateDTO dto) {
        Optional.ofNullable(complaintConfigManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("data.does.not.exist")));
        ComplaintConfigEntity entity = complaintConfigConvert.update(dto);
        return complaintConfigManager.updateById(entity);
    }

    /**
     * 根据id删除 用户投诉规则表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("complaint.config.id.not.blank"));
        return complaintConfigManager.removeById(id);
    }

    /**
     * 根据id删除 用户投诉规则表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(ComplaintConfigDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
